#include "player.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <DynamicArray.h>
#include <MyString.h>

void InitPlayer(player *p)
{
    p->HP = 100;
    p->ATK = 5;
    p->x = 0;
    p->y = 0;
    p->symbol = 'X';
    p->exp = 0;
    p->level = 0;
    p->golden = 0;

    InitDynamicArray(&p->bag);
}

void PrintPlayer(player *p)
{
    printf("玩家当前状态: Level: %d级  %3d❤️  %2d⚔️  %2d🪙\n",p->level,p->HP,p->ATK,p->golden);
}

void PlayerMove(player *p, char (*map)[MAPSZIE])
{
    //将原来的位置清空
    map[p->x][p->y] = '-';
    //InitMap(map);

    char choice = 0;
    printf("请选择玩家移动方向(w,a,s,d,p(进入商店🛒),b(打开背包💼)):");
    scanf(" %c",&choice);
    switch(choice)
    {
        case 'w':
            p->x--;
            if(p->x < 0)
                p->x = 0;
            break;
        case 's':
            p->x++;
            if(p->x >= MAPSZIE)
                p->x = MAPSZIE - 1;
            break;
        case 'a':
            p->y--;
            if(p->y < 0)
                p->y = 0;
            break;
        case 'd':
            p->y++;
            if(p->y >=MAPSZIE)
                p->y = MAPSZIE - 1;
            break;
        case 'p':
            ShopInterFace(p);
            break;
        case 'b':
            ShowBag(p);
        default:
            break;
    }
     
    

    //附上新位置
    map[p->x][p->y] = p->symbol;
}

void LevelUp(player *p)
{
    if(p->exp >= (p->level + 1) * 50)
    {
        p->level++;
        printf("恭喜你升级到%d级\n",p->level);
        p->HP = 100;
        p->ATK += p->ATK * 0.5;
    }
}

void ShopInterFace(player *p)
{
    while(1)
    {
        system("clear");
        printf("-----------------欢迎来到商店!------------------\n");
        printf("请选择你要购买的商品:\n");
        printf("1.村里最好的剑🗡️ (攻击力+5)                  ￥5\n");
        printf("2.村里最好的盔甲🛡️ (防御力+5)                ￥5\n");
        printf("3.小血瓶❤️ (血量+5)                          ￥2\n");
        printf("4.大血瓶💕(血量+10)                         ￥4\n");
        printf("------------------------------------------------\n");
        printf("                                      q.退出商店\n");
        printf("做出你的选择:");
        int choice = 0;
        scanf("%d",&choice);

        switch (choice)
        {
        case 1:
            if(p->golden < 5)
            {
                printf("金币不够,购买失败❌\n");
                break;
            }
            printf("购买武器成功✔️\n");
           p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon("村里最好的剑🗡️",5,-1)));
           p->golden -= 5;
            break;
        case 2:
            if(p->golden < 5)
            {
                printf("金币不够,购买失败❌\n");
                break;
            }
            printf("购买武器成功✔️\n");
           p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,CreateDefence("村里最好的盔甲🛡️",5,-1)));
           p->golden -= 5;
            break;
        case 3:
            if(p->golden < 2)
            {
                printf("金币不够,购买失败❌\n");
                break;
            }
            p->bag.InsertArrayTail(&p->bag,CreateProp(Medicament,CreateMedicament("小血瓶❤️",5,-1)));
            p->golden -= 2;
            break;
        case 4:
            if(p->golden < 4)
            {
                printf("金币不够,购买失败❌\n");
                break;
            }
            p->bag.InsertArrayTail(&p->bag,CreateProp(Medicament,CreateMedicament("大血瓶💕",10,-1)));
            p->golden -= 4;
            break;
        default:
            break;
        }
        break;
    }
    
}

void ShowProp(void *ptr)
{
    prop *p = (prop*)ptr;
    if(p->type == Weapon)
    {
        weapon *w = (weapon*)p->ptr;
        printf("武器名字:|%s |  %d⚔️\n",w->name.string,w->ATK);
    }
    if(p->type == Defence)
    {
        defence *d = (defence*)p->ptr;
        printf("盔甲名字:|%s |  %d🛡️\n",d->name.string,d->def);
    }
    if(p->type == Medicament)
    {
        medicament *m = (medicament*)p->ptr;
        printf("药水名字:|%s |  增加 %d HP\n",m->name.string,m->HP);
    }
}

void ShowBag(player *p)
{
    printf("\n--------------------背包----------------------\n");
    p->bag.DisplayDynamicArray(&p->bag,ShowProp);
    if(p->bag.len == 0)
    {
        printf("                     无                     \n");
    }
    printf("----------------------------------------------\n");
}